<?php
class Front_Preauthorize_Generic extends System_Db_Generic  {
// evaluate as php
	public function getFund(){
		$db = Zend_Registry :: get("db");
		$statement = $db->prepare('exec dbo.sp_queryFundId');
		$statement->execute();
		$results = $statement->fetchAll();
		return $results;
	}
	public function getpreauthbyfund($params){
		$db = Zend_Registry :: get("db");
		switch ($params['searchby']){
			case 'policy':
				$cmd_exec = "exec dbo.sp_elink_member_queryByPolicyNo ".$params['fund_id'].",'".$params['policy_no']."',NULL,NULL";
				break;
			case 'fname' || 'lname':
				$cmd_exec = "exec dbo.sp_elink_member_queryByNameSname ".$params['fund_id'].",'".$params['fname']."','".$params['lname']."'";
				break;
			case 'flname':
				$cmd_exec = "exec dbo.sp_elink_member_queryByPolicyNo ".$params['fund_id'].",'".$params['fname']."','".$params['lname']."'";
				break;
			default: return; break;
		}
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['effective'] = getDateFormat($results[$key]['effective']);
			$results[$key]['expiry'] = getDateFormat($results[$key]['expiry']);
		}
		return $results;
	}
	public function getPreauthId(){
		$db = Zend_Registry :: get("db");
		//$statement = $db->prepare('select dbo.FN_Preauth_GenID () as preauth_id');
		$cmd_exec = "exec dbo.sp_elink_common_GenID 'preauth'";
		$statement = $db->prepare($cmd_exec);
		
		$statement->execute();
		$results = $statement->fetchAll();
		//return $results['0']['preauth_id'];
		return $results['0']['new_id'];
	}
	public function getPolicyById($policy_id){
		if(!$policy_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_member_queryByPolicyID '$policy_id'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['effective'] = getDateFormat2($results[$key]['effective']);
			$results[$key]['expiry'] = getDateFormat2($results[$key]['expiry']);
			$results[$key]['me_cover'] = number_format($results[$key]['me_cover'],2,'.',',');
			$results[$key]['mc_cover'] = number_format($results[$key]['mc_cover'],2,'.',',');
			//$results[$key]['deductible'] = number_format($results[$key]['deductible'],2,'.',',');
			$results[$key]['copayment'] = number_format($results[$key]['copayment'],2,'.',',');			
		}
		return $results['0'];
	}
	public function getPaInfo($policy_id){
		if(!$policy_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_policy_get_info '$policy_id'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();	
		return $results['0'];
	}
	public function getPaClaim($policy_id){
		if(!$policy_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Claim_Get_PA_Claim '$policy_id'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['acc_date'] = getDateFormat2($results[$key]['acc_date']);
			$results[$key]['paid'] = number_format($results[$key]['paid'],2,'.',',');
		}
		return $results;
	}
	public function getPaClaimOpd($policy_id){
		if(!$policy_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Claim_Get_HealthOPD_Claim '$policy_id'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['acc_date'] = getDateFormat2($results[$key]['acc_date']);
			//$results[$key]['paid'] = number_format($results[$key]['paid'],2,'.',',');
			$results[$key]['paid'] = $results[$key]['paid'];
		}
		return $results;
	}
	public function getErClaim($policy_id){
		if(!$policy_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Claim_Get_ER_Claim '$policy_id'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['acc_date'] = getDateFormat2($results[$key]['acc_date']);
			$results[$key]['paid'] = number_format($results[$key]['paid'],2,'.',',');
		}
		return $results;
	}
	public function getPlan2catByPlanId($plan_id,$policy_id){ 
		if(!$plan_id) return;
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_plan2cat_byplanid '$plan_id','$policy_id'"; 
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		//$results['0']['opd'] = number_format($results['0']['opd'],2,'.',',');
		//$results['0']['rb'] = number_format($results['0']['rb'],2,'.',',');
		//$results['0']['er'] = number_format($results['0']['er'],2,'.',',');
		//_print($results); exit();
		return $results['0'];
	}
	public function insertPreauth($params,$preauth_id,$browser,$client_ip){
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_Preauth_Trans_Save '".$preauth_id."','".$params['user_id']."','".$params['policy_no']."','".$params['policy_id']."','".$params['preauth_datetime']."',NULL,'".$params['customer_type_desc']."','".$browser."','".$client_ip."'";
		$statement = $db->prepare($cmd_exec);
		$results = $statement->execute();
		return $results;
	}
	//--beerz
// 	public function updatePreauthImage($img_data){
// 		$db = Zend_Registry :: get("db");
// 		$cmd_exec = "exec dbo.sp_test_update_image '$img_data'";
// 		$statement = $db->prepare($cmd_exec);
// 		$results = $statement->execute();
// 		return $results;
// 	}

	public function getpreauthbyidcard($params){
		$db = Zend_Registry :: get("db");
		$cmd_exec = "exec dbo.sp_elink_member_queryByNatID ".$params['fund_id'].",'".$params['nat_id']."'";
		$statement = $db->prepare($cmd_exec);
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['effective'] = getDateFormat($results[$key]['effective']);
			$results[$key]['expiry'] = getDateFormat($results[$key]['expiry']);
		}
		return $results;
	}
	//--beerz
	public function getDeducList($policy_id){
		$db = Zend_Registry :: get("db");
		$statement = $db->prepare("SELECT * FROM [dbo].[tf_getDeductibleUsed] (   null  ,null  ,null  ,null  ,null  ,'$policy_id'  ,null)");
		$statement->execute();
		$results = $statement->fetchAll();
		foreach ($results as $key=>$value){
			$results[$key]['dgroup'] = getDateFormat2($results[$key]['dgroup']);
			//$results[$key]['deduc'] = number_format($results[$key]['deduc'],2,'.',',');
		}
		return $results;
	}
	
}
?>